﻿@{
    ViewBag.Title = "TimeClassInfo";
}

@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>         
}

<form id="form1" method="post">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" name="SaveInfo" iconCls="icon-save" onclick="SaveInfo" plain="true">保存</a>
                        <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>                
        </div>    
        <div name="Base" title="次卡分类" style="height:100%;overflow:hidden;margin-top:5px;margin-left:20px">
            <table>
                <tr>
                    <td width="80px" height ="28px" align="right">次卡分类：</td>
                    <td width="240px" height ="28px">
                        @Html.Partial("~/Views/Modular/LookupTimeClass.cshtml")
                    </td>
                    <td width="80px" height ="28px" align="right">使用期限：</td>
                    <td width="210px" height ="28px" colspan="2">
                        <input id="FCardType" name="FCardType" class="mini-combobox" style="width:200px;" TabIndex="5" value ="1" data ="CardType" onvaluechanged="TypeChange"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="28px" align="right">门店选择：</td>
                    <td width="200px" height ="28px">
                        @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")
                    </td>
                    <td width="80px" height ="28px" align="right">
                        <div id="typeName">有效天数：</div></td>
                    <td width="210px" height ="28px" colspan="2">
                        <div id="type2" style="display:none;">
                            <table align="left" style="width:240px; left: -3px">
                                 <tr>
                                    <td width ="110px">
                                        <input class="mini-datepicker" name="FDate1" format="yyyy-MM-dd" style="width:110px;" value = ""/></td>
                                    <td width ="16px">至</td>
                                    <td width ="110px">
                                        <input class="mini-datepicker" name="FDate2" format="yyyy-MM-dd" style="width:110px;" value = ""/></td>
                                 </tr>
                            </table>
                        </div>
                        <div id="type1">
                            <input id="FEffectDays" name="FEffectDays" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                                 style="width:200px;" TabIndex="7" showbutton="false"  format="#,0" value="1"/>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="28px" align="right">次卡编码：</td>
                    <td width="200px" height ="28px">
                        <input name="FNumber" id="FNumber" class="mini-textbox" style="width:200px;"  value="" TabIndex="2"/>
                    </td>
                    <td width="80px" height ="28px" align="right">销售价格：</td>
                    <td width="210px" height ="28px" colspan="2">
                        <input id="FAmount" name="FAmount" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                             style="width:200px;" TabIndex="6" showbutton="false"  format="#,0.00" value="0"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="28px" align="right">次卡名称：</td>
                    <td width="200px" height ="28px">
                        <input id="FName" name="FName" class="mini-textbox " style="width:200px;" TabIndex="3"/>
                    </td>
                    <td width="80px" height ="28px" align="right">可用次数：</td>
                    <td width="210px" height ="28px" colspan="2">
                        <input id="FTime" name="FTime" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                            style="width:200px;" TabIndex="7" showbutton="false"  format="#,0" value="1"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="28px" align="right">每次积分：</td>
                    <td width="200px" height ="28px">
                        <input id="FIntegral" name="FIntegral" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                             style="width:200px;" TabIndex="6" showbutton="false"  value="0"/>
                    </td>
                    <td width="80px" height="28px" align="right"><span id="RebateTip">提成金额：</span></td>
                    <td width="210px" height="28px">
                        <input id="FRebateValue" name="FRebateValue" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel="false"
                               style="width:200px;" TabIndex="6" showbutton="false" value="0" numberformat="n"/>/每次
                    </td>
                </tr>
                <tr>

                    <td width="80px" height ="28px" align="right">积分方式：</td>
                    <td width="200px" height ="28px">
                        <input id="FIntType" name="FIntType" class="mini-combobox" style="width:200px;" TabIndex="9" value ="0" data ="dIntegralType"/>
                    </td>
                    <td width="80px" height="28px" align="right">状态：</td>
                    <td width="210px" height="28px">
                        <input id="FState" name="FState" class="mini-combobox" style="width:200px;" TabIndex="9" value="0" data="FStateData" />
                    </td>
                </tr>
                <tr>
                    <td width="80px" height="28px" align="right">备注：</td>
                    <td width="200px" height="28px">
                        <input id="FRemark" name="FRemark" class="mini-textbox " style="width:200px;" TabIndex="10" />
                    </td>
                    <td width="80px" height="28px" align="right"></td>
                    <td width="210px" height="28px">
                        
                    </td>
                </tr>
            </table>
        </div>
    <input name="FItemID" class="mini-hidden" />
    <input name="FClassID" class="mini-hidden" />
</form>
        <div id="tabs1" class="mini-tabs" activeIndex="0"  style="width:100%; height:90%; margin-top: 5px; left: 0px;
        overflow:hidden;border:none;" plain="true" buttons="#tabsButtons">
            <div name="time_goods" title="设置消费商品" style="height:40%;overflow:hidden;font-size:12pt;top: 5px; left: 0px;">
                <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
                    <table>
                        <tr>
                            <td width ="80px" align="right">选择方式: </td>
                            <td width ="90px" ><input class="mini-combobox" width ="90px" id ="FType" name = "FType" align="left" value="0"  
                                data="datetype" onvaluechanged="datatype_changed"/></td>
                            <td width="80px" align="right">消费数量：</td>
                            <td width="70px"> 
                                <input name="FGoodSend" id="FGoodSend" class="mini-spinner" minValue="0" maxValue="99999999" showbutton="false" 
                                    style="width:70px;" align="right"  value="0" selectOnFocus="true"/>
                            </td>
                            <td width="80px" align="right">消费方式：</td>
                            <td width="130px">
                                <input class="mini-combobox" width ="130px" id ="FSendType" name = "FSendType" align="left" value="1"  data="sendtype"/>
                            </td>
                            <td width="200px;">
                                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="ShowGoodsWin" plain="true">新增</a>
                                <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRows()" plain="true">删行</a>
                            </td>
                        </tr>
                    </table>       
                </div>
                <div class="mini-fit" >
                    <div id="datagrid1" class="mini-datagrid" style="width:100%;height:130px;" showPager="false"  pageSize="100"
                            borderStyle="border:1;" url="../Member/GetTimeGoodsList"
                            allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false"  
                            sortMode="client" multiSelect="true">
                        <div property="columns">
                            <div type="checkcolumn" ></div>
                            <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                            <div field="FCode" name="FCode" width="150" headeralign="center" allowsort="true">商品条码</div>
                            <div field="FName" name="FName" width="260" headeralign="center" allowsort="true">商品名称</div>
                            <div field="FSpec" name="FSpec" width="120" headeralign="center" allowsort="true">规格</div>
                            <div field="FUnitName" name="FUnitName" width="50" headeralign="center" allowsort="true">单位</div>                            
                        </div>
                    </div>
                </div>
            </div>
            <div name="time_sends" title="消费送次卡设置" style="height:40%;overflow:hidden;font-size:12pt;top: 5px; left: 0px;">
                <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
                    <table style="width:200px;">
                        <tr>
                            <td width="200px;">
                                <a class="mini-button" id="new_send" name="new" iconCls="icon-new" onclick="addRow_Send()" plain="true">新增</a>
                                <a class="mini-button" id="delrow_send" name="delrow" iconCls="icon-remove" onclick="removeRow_Send()" plain="true">删行</a>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="mini-fit" >
                    <div id="datagrid2" class="mini-datagrid" style="width:100%;height:130px;" showPager="false"
                            borderStyle="border:1;" url="../Member/GetTimeGoodsList"
                            allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false"  
                            sortMode="client" multiSelect="true">
                        <div property="columns">
                            <div type="checkcolumn" ></div>
                            <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                            <div field="FPosAmtMin" name="FPosAmtMin" numberFormat="#,0.00" width="180" headerAlign="center" align="right" summaryType="sum">最低消费多少元
                                <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                            </div>
                            <div field="FPosAmtMax" name="FPosAmtMax" numberFormat="#,0.00" width="180" headerAlign="center" align="right" summaryType="sum">到最高消费多少元
                                <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                            </div>
                            <div field="FGiveAmount" name="FGiveAmount" numberFormat="#,0.00" width="150" headerAlign="center" align="right" summaryType="sum">赠送多少次
                                <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <div id="tabsButtons">
       <div id="ck1" name="ck1"  class="mini-checkbox" style ="margin-right:70px" readOnly="false" value ="true" text="连续新增 "></div>
    </div> 
<script>
    var CardType = [{ "id": "0", "text": "不限制" },
                       { "id": "1", "text": "限制使用天数" },
                    { "id": "2", "text": "固定使用日期" }];
    var FStateData = [{ "id": "0", "text": "正常" },
                        { "id": "1", "text": "停用" }];
    var datetype = [{ "id": "0", "text": "全选" }, { "id": "1", "text": "任选" }];
    var sendtype = [{ "id": "1", "text": "消费时扣减商品" }, { "id": "2", "text": "充值时扣减商品" }];
    var dIntegralType = [{ "id": "0", "text": "充值时积分" },
                    { "id": "1", "text": "消费时积分" }];
    mini.parse();

    var grid = mini.get("datagrid1");
    var grid2 = mini.get("datagrid2");
    function FormShown() {
        var nowDate = new Date();
        mini.getbyName("FDate1").setValue(nowDate);
        mini.getbyName("FDate2").setValue(nowDate);
        mini.getbyName("FGoodSend").setEnabled(false);
    }

    function datatype_changed() {
        if (mini.get("FType").getValue() == "0") {
            mini.getbyName("FGoodSend").setEnabled(false);
            mini.getbyName("FGoodSend").setValue(mini.get("datagrid1").getData().length);
        }
        else
            mini.getbyName("FGoodSend").setEnabled(true);
    }

    function addRow_Send() {
        var newRow = { FEntryID: 0, FPosAmtMin: 0, FPosAmtMax: 0, FGiveAmount: 0, FMemberTimeID: 0 };
        grid2.addRow(newRow, grid2.getData().length);
    }

    function removeRow_Send() {
        var rows = grid2.getSelecteds();
        if (rows.length > 0) {
            grid2.removeRows(rows, true);
        }
    }

    //加载资料
    function SetInfoData(data) {
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            mini.getbyName("FTimeClassID").setValue(data.FClassID);
            mini.getbyName("FTimeClassID").setText(data.FClassNumber);
            mini.getbyName("FTimeClassName").setValue(data.FClassName);
            NewInfo();
        }
        else {
            $.post("GetTimeInfo", { json: "[{'FItemID':'" + data.FItemID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                mini.getbyName("FItemID").setValue(mdata.data[0].FItemID);
                form.setData(mdata.data[0]);
                mini.getbyName("FTimeClassID").setText(mdata.data[0].FTimeClassNumber);
                mini.getbyName("FTimeClassID").setValue(mdata.data[0].FTimeClassID);
                mini.getbyName("FStockID").setText(mdata.data[0].FStockNumber);
                mini.getbyName("FStockID").setValue(mdata.data[0].FStockID);
                mini.getbyName("FNumber").setEnabled(false);
                mini.getbyName("FDate1").setValue(mdata.data[0].FStartDate);
                mini.getbyName("FDate2").setValue(mdata.data[0].FEndDate);
                mini.getbyName("FState").setValue(mdata.data[0].FState);
                setType(mdata.data[0].FCardType);
                mini.getbyName("ck1").setChecked(false);
                mini.getbyName("FType").setValue(mdata.data[0].FType);
                if (mdata.data[0].FType > 0)
                    mini.getbyName("FGoodSend").setEnabled(true);
                mini.getbyName("FGoodSend").setValue(mdata.data[0].FGoodSend);
                mini.getbyName("FSendType").setValue(mdata.data[0].FSendType);
                mini.getbyName("FIntegral").setValue(mdata.data[0].FIntegral);
                mini.getbyName("FIntType").setValue(mdata.data[0].FIntType);
                if (mdata.data[0].IsUse > 0)
                    mini.get("FSendType").setEnabled(false);
                mini.getbyName("FRebateValue").setValue(mdata.data[0].FRebateValue);
                grid2.setData(mdata.gvData);
                grid2.accept(data);
                LoadListData();
            });
        }
    }

    //新增资料
    function NewInfo() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FName").setValue("");
        mini.getbyName("FAmount").setValue("0");
        mini.getbyName("FEffectDays").setValue("0");
        mini.getbyName("FTime").setValue("0");
        mini.getbyName("FRemark").setValue("");
        mini.getbyName("FRebateValue").setValue(0);
        var jsonstr = '{"mSystemAppID":"' + parent.window.getSystemAppID() + '"}';
        $.post('NewTimeInfo', { json: jsonstr }, function (data) {
            mini.getbyName("FNumber").setValue(data);
        });

        $.post("DefaultMTStock", { json: '[{"mSystemAppID":"' + parent.window.getSystemAppID() + '"}]' }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.data.length > 0) {
                mini.getbyName("FStockID").setText(mdata.data[0].FNumber);
                mini.getbyName("FStockID").setValue(mdata.data[0].FItemID);
                mini.getbyName("FStockName").setValue(mdata.data[0].FName);
            }
        });
        mini.get("datagrid1").setData(null);
        mini.getbyName("FSendType").setEnabled(true);
        grid2.setData(null);
    }

    //保存资料
    function SaveInfo() {
        mini.getbyName("SaveInfo").setEnabled(false);
        if (mini.getbyName("FCardType").getValue() == 0) {
            var nowDate = new Date();
            nowDate.setFullYear(1900, 01, 01);
            mini.getbyName("FDate1").setValue(nowDate);
            nowDate.setFullYear(2099, 12, 31);
            mini.getbyName("FDate2").setValue(nowDate);
        }
        if (mini.getbyName("FCardType").getValue() != 1) {
            mini.getbyName("FEffectDays").setValue(0);
        }
        var form = new mini.Form("#form1");
        var data = form.getData(true, false);
        if (!validata(data)) {
            mini.getbyName("SaveInfo").setEnabled(true);
            return;
        }
        data.FType = mini.getbyName("FType").getValue();
        data.FGoodCount = grid.getData().length;
        data.FGoodSend = mini.getbyName("FGoodSend").getValue();
        data.FSendType = mini.getbyName("FSendType").getValue();
        data.goodsRows = grid.getChanges();
        data.giveRows = grid2.getChanges();
        var json = mini.encode(data);
        $.post("SaveTimeInfo", { json: json, mSystemAppID: parent.window.getSystemAppID()}, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                if (mini.getbyName("ck1").getChecked() == false)
                    CloseWindow();
                else
                    NewInfo();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").setEnabled(true);
        });
    }

    function validata(data) {
        if (data.FStockID == '') {
            mini.alert("门店未选择");
            return false;
        }
        if (data.FTimeClassID == '') {
            mini.alert("次卡分类未选择");
            return false;
        }
        if (data.Fnumber == '') {
            mini.alert("编号未输入");
            return false;
        }
        if (data.FName == '') {
            mini.alert("次卡名称不能为空");
            return false;
        }
        if (data.FAmount == '') {
            mini.alert("消费金额未输入");
            return false;
        }
        if (data.FEffectDays == '') {
            mini.alert("有效天数未输入");
            return false;
        }
        if (data.FTime == '') {
            mini.alert("次数未输入");
            return false;
        }
        var goodsend = mini.getbyName("FGoodSend").getValue();
        if (goodsend <= 0 && mini.get("datagrid1").getData().length > 0) {
            mini.alert("消费数量须大于零");
            mini.getbyName("FGoodSend").focus();
            return false;
        }
        if (goodsend > mini.get("datagrid1").getData().length) {
            mini.alert("消费数量不能大于所选商品数");
            mini.getbyName("FGoodSend").focus();
            return false;
        }
        return true;
    }

    function TypeChange() {
        setType(mini.getbyName("FCardType").getValue());
    }

    function setType(typeValue){
        if (typeValue == 0) {
            $("#typeName").html("有效天数：");
            $("#type1").show();
            mini.getbyName("FEffectDays").setEnabled(false);
            mini.getbyName("FEffectDays").setValue(0);
            $("#type2").hide();
        }
        else if (typeValue == 1) {
            $("#typeName").html("有效天数：");
            $("#type1").show();
            mini.getbyName("FEffectDays").setEnabled(true);
            $("#type2").hide();
        }
        else if (typeValue == 2) {
            $("#typeName").html("固定日期：");
            $("#type1").hide();
            $("#type2").show();
        }
    }

    function LoadListData() {
        var sSystemAppID = parent.window.getSystemAppID();
        var classid = mini.getbyName("FItemID").getValue();
        if (classid != "0") {
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + sSystemAppID + "',";
            json = json + "'mClassID':" + "'" + classid + "',";
            json = json + "'mNumber':" + "'',";
            json = json + "'mDatatype':0";
            json = json + "}]";
            grid.load({ mjson: json });
        }
    }

    //删除资料
    function removeRows() {
        var row = grid.getSelecteds();
        if (row) {
            mini.confirm("确定要删除选中的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        SetRemoveRows();
                    }
                });
        } else {
            mini.alert("请选要删除的记录");
        }
    }

    function SetRemoveRows() {
        var FSystemAppID = parent.window.getSystemAppID();
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
        grid.deselectAll();
    }

    //选择商品表
    function ShowGoodsWin() {
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: '', mBillType: 15 };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        var newRow = {
                            FGoodsID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                            FUnitName: rw.FUnitName, FCount: 1, FConvert: rw.FConvert,FCode:rw.FCode
                        };
                        AddRow(newRow);
                        datatype_changed();
                    }
                }
            }
        });
    }

    function ExistsInGrid(FItemID) {
        var ret = false;
        $.each(grid.getData(), function (x, e) {
            if (e.FGoodsID == FGoodsID) {
                ret = true;
                return false;
            }
        });
        return ret;
    }

    function AddRow(row) {
        try {
            if (!ExistsInGrid(row.FGoodsID)) {
                grid.addRow(row, grid.getData().length);
            }
        } catch (e) {
            grid.addRow(row, 0);
        }
    }

</script>
